﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Treinamento2015_01Core.Repository;

namespace Treinamento2015_01Core.Service
{

    public abstract class BaseService<TEntity, TRepository>
        where TEntity : class
        where TRepository : IBaseRepository<TEntity>
    {
        protected TRepository _Repository;

        protected BaseService(IBaseRepository<TEntity> repository)
        {
            _Repository = (TRepository)repository;
        }

        public List<TEntity> FindAll()
        {
            return _Repository.FindAll().ToList();
        }

        public TEntity FindByKey(int entityId)
        {
            return _Repository.FindById(entityId);
        }

        public virtual TEntity Create(TEntity entity)
        {
            SetICreatedProperties(entity);
            TEntity newEntity = _Repository.Create(entity);
            return newEntity;
        }


        public IList<TEntity> Create(IList<TEntity> entities)
        {
            foreach (TEntity entity in entities)
            {
                SetICreatedProperties(entity);
            }
            return _Repository.Create(entities);
        }

        public TEntity Update(TEntity entity)
        {
            SetIModifiableProperties(entity);
            TEntity newEntity = _Repository.Update(entity);
            return newEntity;
        }

        public IList<TEntity> Update(IList<TEntity> entities)
        {
            foreach (TEntity entity in entities)
            {
                SetIModifiableProperties(entity);
            }
            return _Repository.Update(entities);
        }

        public void Delete(int entityId)
        {
            _Repository.Remove(entityId);
        }

        public void Delete(TEntity entity)
        {
            _Repository.Remove(entity);
        }

        public void Delete(IList<TEntity> entities)
        {
            _Repository.Remove(entities);
        }

        public int Count()
        {
            return _Repository.Count();
        }

        public List<TEntity> PagedList(int pageNumber, int pageSize)
        {
            return _Repository.PagedList(pageNumber, pageSize);
        }

        private void SetICreatedProperties(TEntity entity)
        {
            //ICreated created = entity as ICreated;
            //if (created != null)
            //{
            //   // User currentUser = new UserService().GetCurrentUser();
            //    DateTime actualTime = DateTime.Now;

            //   // created.CreatedBy = currentUser;
            //    created.CreatedAt = actualTime;
            //   // created.ModifiedBy = currentUser;
            //    created.ModifiedAt = actualTime;
            //}
        }

        private void SetIModifiableProperties(TEntity entity)
        {
            //ICreated modifiable = entity as ICreated;
            //if (modifiable != null)
            //{
            //    //User currentUser = new UserService().GetCurrentUser();
            //    DateTime actualTime = DateTime.Now;
            //    //modifiable.ModifiedBy = currentUser;
            //    modifiable.ModifiedAt = actualTime;
            //}
        }
    }
}